package net.alpenviewer.client.ui;

import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;

/**
 * <pre>
 * +-------------------------+
 * |      head               |
 * +-------------------------+
 * |      |  center top      |
 * | left |------------------|
 * |      |  center bottom   |
 * +-------------------------+
 * </pre>
 *
 */
public class RootLayout extends DockLayoutPanel {

	private static final Unit LAYOUT_UNIT     = Unit.PX;
	private static final int  HEAD_SIZE       = 40;
	private static final int  LEFT_SIZE       = 200;
	private static final int  MIN_CENTER_SIZE = 200;

	private final HeadPanel         headPanel;
	private final LeftPanel         leftPanel;
	private final CenterTopPanel    centerTopPanel;
	private final CenterBottomPanel centerBottomPanel;

	public RootLayout() {
		this(LAYOUT_UNIT);
	}
	
	protected RootLayout(Unit unit) {
		super(unit);		

		// calculate the size for center panel		
		int centerSize = MIN_CENTER_SIZE;
		{
			final int _centerSize = ( Window.getClientHeight() - HEAD_SIZE ) / 2;
			centerSize = _centerSize < MIN_CENTER_SIZE ? MIN_CENTER_SIZE : _centerSize;	
		}
		
		// the enclosing panels		
		headPanel         = new HeadPanel();
		leftPanel         = new LeftPanel();
		centerTopPanel    = new CenterTopPanel();
		centerBottomPanel = new CenterBottomPanel();	

		// add head panel, and let it overflow on other panel
		this.addNorth(headPanel, HEAD_SIZE);
	    this.getWidgetContainerElement(headPanel).getStyle().setZIndex(2);
	    this.getWidgetContainerElement(headPanel).getStyle().setOverflow(Overflow.VISIBLE);
	    
		// warp other panels in a SplitLayoutPanel 
		SplitLayoutPanel splitLayout = new SplitLayoutPanel();		
		{
			splitLayout.addWest (leftPanel,      LEFT_SIZE);
			splitLayout.addNorth(centerTopPanel, centerSize);
			splitLayout.add     (centerBottomPanel);
		}
		this.add(splitLayout);
		
	}
	
}
